/*
 * Copyright 2016-2036 the original author or authors.
 *
 * COMMERCIAL USE OF THIS SOFTWARE WITHOUT WARRANTY IS NOT ALLOWED.
 * Use is subject to license terms! You can distribute a copy of this software
 * to others for free. This software is a non-profit and open-source project.
 * Any contribution to this project will make it better.
 * All rights reserved! Owned by Stephen Liu.
 *
 */
package com.github.hingo

import com.github.hingo.entity.Course
import com.github.hingo.service.CourseService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.dao.DuplicateKeyException
import org.springframework.test.annotation.Rollback
import spock.lang.Title

/**
 * @author ste7en.liu@gmail.com
 * @since 2016/10/22
 */
@Title("Course Test")
class CourseSpecTest extends SpockSpec {

    @Autowired
    CourseService courseService

    @Rollback
    def "Test add course"() {

        given: "Get ready for testing"
            def course = new Course()
            course.code = "D400013"
            course.name = "政治经济学"
            course.score = 4
            course.gmtCreated = new Date()
            course.gmtModified = new Date()

        when: "Add a new course"
            courseService.add(course)
            def result = courseService.findOne(course.code)

        then: "Course should be successfully added into database"
            result != null
            result.code == course.code
            result.name == course.name
            result.score == course.score

        when: "Add a course that has already existed"
            courseService.add(course)

        then: "Exception should occur"
            thrown(DuplicateKeyException.class)
    }
}
